package com.metronoteapp.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Filterable;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.metronoteapp.R;
import com.metronoteapp.dialog.DialogDetailNoteClient;
import com.metronoteapp.dialog.PlayingAudioDialog;
import com.metronoteapp.model.NoteModel;
import com.metronoteapp.until.Constants;

public class AllNoteAdapter extends BaseAdapter implements OnItemClickListener {
	private String TAG = AllNoteAdapter.class.getSimpleName();
	private LayoutInflater mInflater;
	private List<NoteModel> mData = null;
	private List<NoteModel> mDataSearch;
	private Context mContext;

	public AllNoteAdapter(Context context, List<NoteModel> _data) {
		mContext = context;
		mInflater = LayoutInflater.from(context);
		mData = _data;
		this.mDataSearch = new ArrayList<NoteModel>();
		this.mDataSearch.addAll(mData);
		Log.v(TAG, "AllNoteAdapter() | : " + mData.size());
	}

	public class ViewHolder {
		TextView txtTitle;
		TextView txtStart;
		TextView txtDetail;
		TextView txtIcon;
		LinearLayout lnText;
		LinearLayout lmVoice;
	}

	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		NoteModel temp = mData.get(position);
		Log.v(TAG, "getView() | Set item : " + position + " | "
				+ temp.getTitle().toString());

		final ViewHolder holder;
		if (view == null) {
			holder = new ViewHolder();
			view = mInflater.inflate(R.layout.layout_metro_note_row, null);
			holder.txtTitle = (TextView) view.findViewById(R.id.txtTitle);
			holder.txtStart = (TextView) view.findViewById(R.id.txtStart);
			holder.txtDetail = (TextView) view.findViewById(R.id.txtDetail);
			holder.txtIcon = (TextView) view.findViewById(R.id.txtIconTitle);
			holder.lnText = (LinearLayout) view.findViewById(R.id.paneText);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		holder.txtTitle.setText(temp.getTitle().toString() + "("
				+ temp.getDateTime() + ")");
		if (temp.getFlgFav() == 1) {
			holder.txtStart
					.setBackgroundResource(R.drawable.start_important_select);
		} else {
			holder.txtStart.setBackgroundResource(R.drawable.start_important);
		}
		if (temp.getNoteType().equalsIgnoreCase(Constants.TYPE_TEXT)) {
			holder.txtDetail.setText(temp.getContent().toString());
			holder.txtIcon.setBackgroundResource(R.drawable.text_icon);
		} else if (temp.getNoteType().equalsIgnoreCase(Constants.TYPE_AUDIO)) {
			holder.txtDetail.setText(temp.getContent().toString());
			holder.txtIcon.setBackgroundResource(R.drawable.voice_icon);
		} else if (temp.getNoteType().equalsIgnoreCase(Constants.TYPE_PICTURE)) {
			Log.v(TAG, "Location: " + temp.getContent().toString());
			holder.txtIcon.setBackgroundResource(R.drawable.image_icon);
			holder.txtDetail.setText(spilitPath(temp.getContent().toString()));
		}
		return view;
	}

	@Override
	public int getCount() {
		try {
			return mData.size();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	// Call edit infor screen
	public void callEditScreen(int _position) {
		try {

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	// Return holder selected by button check
	public ViewHolder getViewHolder(View v) {
		if (v.getTag() == null) {
			return getViewHolder((View) v.getParent());
		}
		return (ViewHolder) v.getTag();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		NoteModel tempModel = mData.get(arg2);
		Log.v(TAG, "Clicked : " + tempModel.getTitle());
		if (tempModel.getNoteType().equalsIgnoreCase(Constants.TYPE_TEXT)) {
			DialogDetailNoteClient objDialog = new DialogDetailNoteClient(
					mContext);
			objDialog.displaydialog(String.valueOf(tempModel.getId()),
					tempModel.getTitle(), tempModel.getContent(),
					String.valueOf(tempModel.getFlgFav()),
					tempModel.getDateTime());
		} else if (tempModel.getNoteType().equalsIgnoreCase(
				Constants.TYPE_AUDIO)) {
			PlayingAudioDialog objPlaying = new PlayingAudioDialog(mContext);
			objPlaying.displaydialog(tempModel.getContent(),
					tempModel.getTitle(), tempModel.getDateTime());
		}
	}

	public void filter(CharSequence s) {
		mData.clear();
		if (s.length() == 0) {
			mData.addAll(mDataSearch);
		} else {
			for (NoteModel wp : mDataSearch) {
				if (wp.getTitle().toLowerCase(Locale.getDefault()).contains(s)) {
					mData.add(wp);
				}
			}
		}
		notifyDataSetChanged();
	}

	public String spilitPath(String _path) {
		String strResult = "";
		int i = 0;
		int j = 0;
		char[] tempList = _path.toCharArray();
		for (char output : tempList) {
			i = i + 1;
			if (output == '|') {
				j = i;
				break;
			}
		}
		strResult = _path.substring(j, tempList.length);
		Log.v(TAG, "Location: " + strResult);
		return strResult;
	}
}